//
//  NGDBHelper.h
//  NetMobileLocation
//
//  Created by Mehmet Sinan Şahin on 1/22/12.
//  Copyright (c) 2012 netgis. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "/usr/include/sqlite3.h"
#import "NGUtility.h"

@class NGLocation;
@class Message;

extern NSString * const TABLE_NAME_LOCATIONS;
extern NSString * const TABLE_NAME_MESSAGES;

@interface NGDBHelper : NSObject {
    NSString *databasePath;
}

@property (nonatomic, retain) NSString *databasePath;

+ (NSString*)path;

/**
 * Opens database and returns singleton instance
 */
+ (NGDBHelper*) openDatabase;
+ (BOOL)purge;

#pragma mark - Location methods
//- (BOOL) insertLocation:(NGLocation *)location;
- (BOOL) insertLocationForLog:(NGLocation *)location;
- (BOOL) insertLocationForUnsent:(NGLocation *)location;

//Search db with locations' tarihSaat
- (BOOL) existsLocation:(NGLocation *)location logType: (NGLOC_LOG_TYPE) logType;
- (BOOL) deleteLocations: (NGLOC_LOG_TYPE) logType;
- (BOOL) deleteAllLocations;

/*
 * deletes location record, maybe SENT field can be updated as 1
 */
- (BOOL) sendLocation:(int)_id; 
- (BOOL) sendLocations: (NSMutableArray *)locations;
- (long) howManyLocations: (NGLOC_LOG_TYPE) logType;

- (NSMutableArray *)listLocations: (NSString *)whereSql;
- (NSMutableArray *)listUnsentLocations;
- (NSMutableArray *)listLoggedLocations;

#pragma mark - Message methods
- (BOOL) insertMessage:(Message *)message;
- (BOOL) insertMessages:(NSMutableArray *)messages;
- (NSMutableArray *)listMessages:(MESSAGE_TYPE) type;
- (NSMutableArray *)listAllMessages;
- (BOOL) deleteAllMessages;
- (BOOL) deleteMessage: (int []) ids;
- (BOOL) markMessageAsRead: (int)_id;
- (BOOL) deleteMessageWithId: (int) _id;
- (long) howManyMessages;
- (long) howManyNewMessages;

#pragma mark - General methods
/**
 * if where sql is nil, no effect
 * pass whereSql as exclude where
 */
- (long) getRecordSize:(NSString *)tableName whereSql:(NSString *)whereSql;

- (NSMutableArray *) getOldestId:(NSString *)tableName  size:(int)size;

@end
